<script setup>
import { Delete, Edit } from '@element-plus/icons-vue'
import ChannelSelect from './components/ChannelSelect.vue'
import ArticleEdit from './components/ArticleEdit.vue'
import { formatTime } from '@/utils/format'
import { ref } from 'vue'
import { artDel, artList } from '@/api/article'
// 假数据
const articleList = ref([])
const loading = ref(false)
const form = ref()
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})
const total = ref(0)
const getList = async () => {
  loading.value = true
  const res = await artList(params.value)
  articleList.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
getList()
const handleCurrentChange = (page) => {
  params.value.pagenum = page
  getList()
}
const handleSizeChange = (size) => {
  params.value.pagenum = 1
  params.value.pagesize = size
  getList()
}
const handleSearch = () => {
  params.value.pagenum = 1
  getList()
}
const handleReset = () => {
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getList()
}
const handleEdit = (row) => {
  form.value.open(row)
}
const handleAdd = () => {
  form.value.open({})
}
const handleSuccess = (type) => {
  if (type === 'add') {
    const lastPage = Math.ceil((total.value + 1) / params.value.pagesize)
    params.value.pagenum = lastPage
  }
  getList()
}
const handleDelete = async (row) => {
  await ElMessageBox.confirm('你确认删除该文章信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDel(row.id)
  ElMessage({ type: 'success', message: '删除成功' })
  getList()
}
</script>
<template>
  <page-container title="文章管理">
    <template #extra>
      <el-button type="primary" @click="handleAdd">发布文章</el-button>
    </template>
    <el-form inline>
      <el-form-item label="文章分类：">
        <channel-select
          style="width: 150px"
          v-model="params.cate_id"
        ></channel-select>
      </el-form-item>
      <el-form-item label="发布状态：">
        <el-select style="width: 150px" v-model="params.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="articleList" style="width: 100%" v-model="loading">
      <el-table-column label="文章标题" width="180">
        <template #default="scope">
          <el-link type="primary" :underline="false">{{
            scope.row.title
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column
        prop="cate_name"
        label="分类"
        width="180"
      ></el-table-column>
      <el-table-column prop="pub_date" label="发表时间">
        <template #default="scope">
          {{ formatTime(scope.row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column prop="state" label="状态"></el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button
            type="primary"
            :icon="Edit"
            circle
            plain
            @click="handleEdit(scope.row, scope.$index)"
          ></el-button>
          <el-button
            type="danger"
            :icon="Delete"
            circle
            plain
            @click="handleDelete(scope.row, scope.$index)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 3, 5, 10]"
      :background="true"
      layout="jumper, total, sizes, prev, pager, next"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    />
    <article-edit ref="form" @success="handleSuccess"></article-edit>
  </page-container>
</template>
